package edu.zju;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class PortFinder {
	public String perlScript;
	
	public PortFinder(String perlScript){
		this.perlScript = perlScript + "findusb.pl";
//		System.out.println("init port finder script:" + this.perlScript);
	}
	
	public Map<String,String> find(){
		return this.find(this.perlScript);
	}
	
	public Map<String,String> find(String perl){
		   String[] cmd = { "/usr/bin/perl", this.perlScript};
	        StringBuffer resultStringBuffer = new StringBuffer();
	        String lineToRead = "";
	        int exitValue = 0;
	          Map<String,String> m = new HashMap<String,String>();
	        System.out.println("Finding ports with " + this.perlScript);
	        try {
	            Process proc = Runtime.getRuntime().exec(cmd);
	            InputStream inputStream = proc.getInputStream();
	            BufferedReader bufferedRreader = new BufferedReader(new InputStreamReader(inputStream));
	            // save first line
	            if ((lineToRead = bufferedRreader.readLine()) != null) {
	            	System.out.println(lineToRead);
	                resultStringBuffer.append(lineToRead);
	            }
	            // save next lines
	            while ((lineToRead = bufferedRreader.readLine()) != null) {
	            	System.out.println(lineToRead);
	                resultStringBuffer.append("\r\n");
	                resultStringBuffer.append(lineToRead);
	            }

	            // Always reading STDOUT first, then STDERR, exitValue last
	            proc.waitFor(); // wait for reading STDOUT and STDERR over
	            exitValue = proc.exitValue();
	        } catch (Exception ex) {
	        	ex.printStackTrace();
	            resultStringBuffer = new StringBuffer("");
	            exitValue = 2;
	        }

//	        System.out.println("exit:" + exitValue);
	        if (exitValue == 0){
	          String[] lines = resultStringBuffer.toString().split("\n");
	          for (int i = 0; i < lines.length; i++){
	            String[] parts = lines[i].split("\\s+");
	            m.put(parts[0].trim(),parts[2]);
		        System.out.println(parts[0].trim() +" " + m.get(parts[0].trim()));
	          }
	        }else{
	          System.out.println("final str:" + resultStringBuffer.toString()  + "\n");
	        }
	        return m;
	    }
	
//    public static void main(String args[]) {
//        String[] cmd = { "/usr/bin/perl", "findusb.pl"};
//        StringBuffer resultStringBuffer = new StringBuffer();
//        String lineToRead = "";
//        int exitValue = 0;
//        try {
//            Process proc = Runtime.getRuntime().exec(cmd);
//            InputStream inputStream = proc.getInputStream();
//            BufferedReader bufferedRreader = new BufferedReader(new InputStreamReader(inputStream));
//            // save first line
//            if ((lineToRead = bufferedRreader.readLine()) != null) {
//                resultStringBuffer.append(lineToRead);
//            }
//            // save next lines
//            while ((lineToRead = bufferedRreader.readLine()) != null) {
//                resultStringBuffer.append("\r\n");
//                resultStringBuffer.append(lineToRead);
//            }
//
//            // Always reading STDOUT first, then STDERR, exitValue last
//            proc.waitFor(); // wait for reading STDOUT and STDERR over
//            exitValue = proc.exitValue();
//        } catch (Exception ex) {
//            resultStringBuffer = new StringBuffer("");
//            exitValue = 2;
//        }
//
//        if (exitValue == 0){
//        //System.out.println("exit:" + exitValue);
//          String[] lines = resultStringBuffer.toString().split("\n");
//          Map<String,String> m = new HashMap<String,String>();
//          for (int i = 0; i < lines.length; i++){
//            String[] parts = lines[i].split("\\s+");
//            m.put(parts[0].trim(),parts[2]);
//          }
//        }else{
//          System.out.println(resultStringBuffer.toString());
//        }
//    }
}
